// pages/index/index.js
var util = require('../../utils/dataformat');
Page({
  data: {
    currentIndex:0,
    startCity:'北京',
    endCity:'上海',
    adultNum: 1,
    childNum: 1,
    rotate: -180,
    date: '8月13日',
    // goDate: '出发时间',
    // backDate: '到达时间',
    show1: false,
    show2: false,
  },
  onLoad: function(op){
    let city= getApp().city
    let type= getApp().type
    if (type === 'go') {
    this.setData({
      startCity: city
    })
    } else if (type === 'back') {
      this.setData({
        endCity: city
      })
    }
    var time = util.formatTime(new Date());
    console.log(time);
    console.log(time.slice(5,10));
  },
  onClose1() {
    this.setData({ show1: false });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getMonth() + 1}月${date.getDate()}日`;
  },
  onConfirm1(event) {
    this.setData({
      show1: false,
      date: this.formatDate(event.detail),
      // goDate: this.formatDate(event.detail),
    });
  },
  
  chooseGo() {
    this.setData({ show1: true })
    // if (this.data.currentIndex == '0') {
    //   this.setData({ show1: true });
    // } else {
    //   this.setData({ show2: true });
    // }
  },
  // chooseBack() {
  //     this.setData({ show2: true });
  // },
  // onClose2() {
  //   this.setData({ show2: false });
  // },
  // onConfirm2(event) {
  //   const [start, end] = event.detail;
  //   this.setData({
  //     show2: false,
  //     date: `${this.formatDate(start)} - ${this.formatDate(end)}`,
  //    goDate: this.formatDate(start),
  //   //  backDate: this.formatDate(end),
  //   });
  // },
  onReady: function(){
    this.animation = wx.createAnimation({
      timingFunction: "ease",
      duration: 400
    })
  },

  minusAdultNum: function(){//减少成人的数量
    if(this.data.adultNum > 0){
      this.data.adultNum--;
      this.setData({
        adultNum: this.data.adultNum
      })
    }
  },
  plusAdultNum: function(){//增加成人的数量
    this.data.adultNum++
    this.setData({
      adultNum: this.data.adultNum
    })
  },

  minusChildNum: function(){//减少儿童的数量
    if(this.data.childNum > 0){
      this.data.childNum--;
      this.setData({
        childNum: this.data.childNum
      })
    }
  },
  plusChildNum: function(){//增加儿童的数量
    this.data.childNum++
    this.setData({
      childNum: this.data.childNum
    })
  },

  rotate_img: function() {//旋转飞机图片
    this.animation.rotateZ(this.data.rotate).step()
    this.setData({
      rotate: -1*this.data.rotate,
      startCity: this.data.endCity,
      endCity: this.data.startCity,
      animation: this.animation.export()
    })
  },
  changeBtn: function(ev) {//单程，往返切换
    console.log(ev);
    // if (ev.target.dataset.index == '1') {
    //    this.setData({
    //      backDate: ''
    //    })
    // }
    this.setData({
      currentIndex: ev.target.dataset.index
    })
  },

  seatchProduct: function(){
      console.log(this.data);
    wx.navigateTo({
      url: `../flight_list/index?from=${this.data.startCity}&to=${this.data.endCity}&date=${this.data.date}&type=${this.data.currentIndex}`
    })
  },



  selectCity:function(e){//选择城市
    console.log(e);
    let type = ''
    if (e.currentTarget.dataset.type === 'go') {
      type = 'go'
    } else {
      type = 'back'
    }
    console.log('跳转城市选择页面');
    wx.navigateTo({
      url: `../city/city?type=${type}`
    })
  }

})